using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.Office.Project.EPMSync.Config.Library;
using System.Globalization;
using Microsoft.Office.Project.EPMSync.Service.Monitor.Properties;

namespace Microsoft.Office.Project.EPMSync.Service.Monitor
{
    public partial class FormQueueDetails : Form
    {
        SqlConnection _sqlConnection;

        public FormQueueDetails( SqlConnection sqlConnection)
        {
            InitializeComponent();
            _sqlConnection = sqlConnection;
        }

        private void frmQueueDetails_Load(object sender, EventArgs e)
        {
            FillGrid("EXEC esGET_QUEUE_ATTRIBUTE_EVENTS", dataGridViewPending);
            FillGrid("EXEC esGET_QUEUE_ATTRIBUTE_EVENTS_ARCHIVE", dataGridViewArchive);
        }

        private bool FillGrid(string QueryString, DataGridView dgv)
        {
            bool successful = true;
            try
            {
                SqlCommand command = new SqlCommand(QueryString, _sqlConnection);

                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        dgv.Rows.Add(AddPendingViewRow(reader[0].ToString(), reader[1].ToString(), reader[2].ToString(), reader[3].ToString(), reader[4].ToString()));
                    }
                }
                reader.Close();

            }
            catch (Exception ex)
            {
                RightAwareMessageBox.Show(String.Format(CultureInfo.InvariantCulture, Resources.ExceptionDBConnect, ex.Message), 
                    Resources.MonitorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                successful = false;
            }

            return successful;
        }

        private static DataGridViewRow AddPendingViewRow(string ID, string AttributeName, string ProjectName, string OldValue, string NewValue)
        {
            DataGridViewRow eventRow = new DataGridViewRow();

            // Add ID
            DataGridViewTextBoxCell id = new DataGridViewTextBoxCell();
            id.Value = ID;
            eventRow.Cells.Add(id);

            // Add AttributeName
            DataGridViewTextBoxCell attributeName = new DataGridViewTextBoxCell();
            attributeName.Value = AttributeName;
            eventRow.Cells.Add(attributeName);

            // Add ProjectName
            DataGridViewTextBoxCell projectName = new DataGridViewTextBoxCell();
            projectName.Value = ProjectName;
            eventRow.Cells.Add(projectName);

            // Add OldValue
            DataGridViewTextBoxCell oldValue = new DataGridViewTextBoxCell();
            oldValue.Value = OldValue;
            eventRow.Cells.Add(oldValue);

            // Add NewValue
            DataGridViewTextBoxCell newValue = new DataGridViewTextBoxCell();
            newValue.Value = NewValue;
            eventRow.Cells.Add(newValue);

            return eventRow;
        }

    }
}